package com.walter.learn.Util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * @ProjectName: learnDemo
 * @Package: com.walter.util
 * @ClassName: TextToSpeechUtil
 * @Author: whoops
 * @Description: 将文本使用语音读出(需要jacob - 1.19 - x64.dll 文件)和依赖jacob.jar,所需jar包都在needJar文件夹下
 * @Date: 2019/8/2 16:53
 * @Version: 1.0
 */
@Slf4j
public class TextToSpeechUtil {

    @Test
    public void main() {
        String filePath = "E:\\hello.txt";
        readText(filePath, 100, 0);
    }

    /**
     * @Description 文本阅读
     * @Param filePath 文本路径
     * @Create 2019/8/2 17:11
     * @Author wangzheng
     */
    public static void readText(String filePath, int volume, int rate) {
        readTxtFile(filePath, volume, rate);
    }

    /**
     * @Description 读取txt文本内容
     * @Param filePath 文本路径
     * @Param volume 音量 0-100
     * @Param rate 语音朗读速度 -10 到 +10
     * @Create 2019/8/2 17:14
     * @Author wangzheng
     */
    public static void readTxtFile(String filePath, int volume, int rate) {
        try {
            String encoding = "GBK";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { // 判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    speak(lineTxt, volume, rate);
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }

    }

    /**
     * @Description 语音输出读取的文件
     * @Param value 文本内容
     * @Param volume 音量 0-100
     * @Param rate 语音朗读速度 -10 到 +10
     * @Create 2019/8/2 17:15
     * @Author wangzheng
     * @Return
     */
    private static void speak(String value, int volume, int rate) {
        // ？？ 这个Sapi.SpVoice是需要安装什么东西吗，感觉平白无故就来了
        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
        // Dispatch是做什么的？
        Dispatch sapo = (Dispatch) sap.getObject();
        try {
            sap.setProperty("Volume", new Variant(volume));
            sap.setProperty("Rate", new Variant(rate));
            Variant defalutVoice = sap.getProperty("Voice");
            Dispatch dispdefaultVoice = (Dispatch) defalutVoice.toDispatch();
            Variant allVoices = Dispatch.call(sapo, "GetVoices");
            Dispatch dispVoices = (Dispatch) allVoices.toDispatch();
            Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
            ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
            ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);
            Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
            // 执行朗读
            Dispatch.call(sapo, "Speak", new Variant(value));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sapo.safeRelease();
            sap.safeRelease();
        }
    }

}
